package src;

import java.io.File;
import java.io.IOException;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequencer;

/** Plays a midi file provided on command line */
public class MidiPlayer {
	PositSlider p;
	Sequencer sequencer;
	
	
	public void playMidi(String f){
		
		String file = f;
        if(!file.equals("")){
        File midiFile = new File(file);
        // Play once
        
        	try {
        		if(sequencer != null){
        			sequencer.stop();
        			sequencer.close();
        		}
				sequencer = MidiSystem.getSequencer();
				Checker checker;
	            sequencer.setSequence(MidiSystem.getSequence(midiFile));
	            sequencer.open();
	            sequencer.start();
	           
	            p =  new PositSlider(sequencer);
	            p.setSize(1000, 20);
	            checker = new Checker(sequencer, p);
	            checker.setSong(sequencer.getTickLength());
	            
	            checker.start();
	          //  p.setSize(500,100);
	           
	       
			} catch (MidiUnavailableException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidMidiDataException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
            
	}
	
	public Sequencer getSequencer(){
		return this.sequencer;
	}

}